<style lang="less">
	@import "../../assets/style/photo.less";
</style>

<template>
	<div>
		<!-- 拍照 start-->
		<van-row>
			<van-col span="24" v-for="(item,index) in photoLists" :key="index" class="col_item">
				<div class="photo_item clearfix">
					<label :for="item.title"><em v-show="isRequired(index+1)">*</em> {{item.title}}</label>
					<span v-show="isEnd">
						<span class="img_box clearfix" v-if="!type && pohtoShow.img">
							<input @change="addPhotos($event,index, item.title)" type="file" accept="image/*" class="kyc-passin">
							<img :src="photoImg" alt="" class="icon_allow" style="width:50px;float:left;">
						</span>
						<span class="img_box clearfix" v-if="type">
							<img @click="addPhotos($event,index,item.title)" :src="photoImg" alt="" class="icon_allow" style="width:50px;float:left;">
						</span>
						<span class="img_box clearfix" v-if="!type && !pohtoShow.img">
							<img @click="allImagesShow('noPhotos')" :src="photoImg" alt="" class="icon_allow" style="width:50px;float:left;">
						</span>
					</span>
				</div>
				<div class="appendPhoto task_takePhoto">
					<div class="img_boxs">
						<img
							class="img_pho"
							v-if="item.photo && item.photo.length>0"
							v-for="(img,i) in item.photo"
							:src="img.photo"
							:key="i"
							@touchstart="longtouch(img,i,index)"
							@touchend="longtouched(img,i)"
							@click="previewImg(item.photo,i)"/>
					</div>
				</div>
			</van-col>
		</van-row>
		<!-- 拍照 end-->
	</div>
</template>

<script>
	import photoImg from '../../assets/img/changguixunshi/icon_allow.png';
	import photoImg2 from '../../assets/img/changguixunshi/icon_dispho.png'
	import { fileData, callData, fileUrl } from '../../axios/axios'
	import { openCamera, getLocation } from '../../api/android'
	import {
		Row,
		Col,
		Dialog,
		Notify,
		ImagePreview
	} from 'vant'

	export default {
		data() {
			return {
				type: false,
				photoImg: null,
				taskType: 0
			}
		},
		components: {
			[Row.name]: Row,
			[Col.name]: Col,
			[Notify.name]: Notify
		},
		props: {
			photoLists: {
				type: Array,
				default() {
					return []
				}
			},
			asterisk: {
				type: Array,
				default() {
					return []
				}
			},
			pohtoParam: {
				type: Object,
				default() {
					return {}
				}
			},
			pohtoShow: {
				type: Object,
				default() {
					return {}
				}
			},
			isEnd: {
				type: Boolean,
				default: true
			}
		},
		methods: {
			isRequired(index) {
				var found = this.asterisk.find(function(element) {
					return element == index;
				});
				return found ? true : false;
			},
			// 是否是安卓
			isAndroid() {
				if(window.Cordova) this.type = true
				else this.type = false
			},
			getPhotoImg() {
				if(!this.pohtoShow.img) this.photoImg = photoImg2
				else this.photoImg = photoImg
			},
			// 长按事件
			longtouch(item,i,index){
				let _this = this
				clearTimeout(this.Loop)
				this.Loop = setTimeout(function(){
					_this.deleteImg(item,i,index)
				},500);
				return false;
			},
			longtouched(){
				clearTimeout(this.Loop)
			},
			// 删除照片
			deleteImg(item,i,index){
				let _this = this
				Dialog.alert({
					showCancelButton: true,
					title: '删除照片提醒',
					message: "是否删除该照片，删除后将无法恢复！"
				}).then(() => {
					let param = {}
					if(_this.pohtoShow.type == 'cgxsIsDw'){
						param.taskType = 0
					}else {
						param.taskType = item.taskType
					}
						param.filePath = item.path
					callData('/fileserver/DeletePicture/deletePicture', 'delete', param).then(res => {
						if(res.success){
							Notify({
								message: '删除成功',
								duration: 1000,
								background: '#ff4d4d'
							});
							_this.echoImages();
							var photoLists = _this.photoLists;
							photoLists[index].photo.splice(i,1);
						}else{
							Notify({
								message: '删除失败',
								duration: 1000,
								background: '#ff976a'
							})
							_this.echoImages()
						}
					})
				});
			},
			// 照片预览
			previewImg(img,index){
				let imgArr = []
				img.forEach((item) => {
					if(item.photo) imgArr.push(item.photo)
				})
				if(imgArr.length != 0){
					ImagePreview({
						images: imgArr,
						startPosition: index
					})
				}
			},
			// 拍照
			addPhotos(ev, index, name) {
				let _this = this
				if(window.Cordova) { // 调取安卓
					getLocation(res => { // 先定位
						if(res.success) {
							openCamera(res1 => { // 再拍照
								if(res1.success) {
									// 再上传
									if(_this.pohtoShow.type == 'cgxs') _this.cgxsPhotos(index, name, res1.data.image_path, res.data)
									else if(_this.pohtoShow.type == 'cgxsIsDw') _this.isDwPhotos(index, name, res1.data.image_path, res.data) // 不到位拍照
									else if(_this.pohtoShow.type == 'bdzs') _this.bdzsPhotos(index, name, res1.data.image_path, res.data)
									else if(_this.pohtoShow.type == 'tsxs') _this.tsxsPhotos(index, name, res1.data.image_path, res.data)
									else if(_this.pohtoShow.type == 'xljx') _this.xljxPhotos(index, name, res1.data.image_path, res.data)		
								}
							})
						}
					})
				} else { // 前端本地上传
					if(_this.pohtoShow.type == 'cgxs') _this.cgxsPhotos(index, name, ev.target.files[0])
					else if(_this.pohtoShow.type == 'bdzs') _this.bdzsPhotos(index, name, ev.target.files[0])
					else if(_this.pohtoShow.type == 'tsxs') _this.tsxsPhotos(index, name, ev.target.files[0])
					else if(_this.pohtoShow.type == 'xljx') _this.xljxPhotos(index, name, ev.target.files[0])
					else if(_this.pohtoShow.type == 'cgxsIsDw') _this.isDwPhotos(index, name, ev.target.files[0])// 不到位拍照
				}
			},
			// 常规巡视拍照
			cgxsPhotos(index, name, filePath, jdwd) {
				this.taskType = 0
				if(window.Cordova) {
					let param = {}
					param.processId = index + 1
					param.fileType = 0
					param.processName = name
					param.taskId = this.pohtoParam.taskId
					param.towerId = this.pohtoParam.towerId
					param.towerTaskId = this.pohtoParam.towerTaskId
					param.jd = jdwd.lon
					param.wd = jdwd.lat
					fileData('/fileserver/PictureZcXs/tsXsUploadPicture', 'post', param, filePath).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath,
							'taskType':0
						})
					})
				} else {
					let param = new FormData();
					param.append("processId", index + 1)
					param.append("fileType", 0)
					param.append("processName", name)
					param.append('taskId', this.pohtoParam.taskId)
					param.append('towerId', this.pohtoParam.towerId)
					param.append('towerTaskId', this.pohtoParam.towerTaskId)
					param.append('jd', '00.000000')
					param.append('wd', '00.000000')
					param.append('file', filePath)
					fileData('/fileserver/PictureZcXs/tsXsUploadPicture', 'post', param).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath,
							'taskType':0
						})
					})
				}
			},
			// 常规巡视不到位拍照
			isDwPhotos(index, name, filePath, jdwd) {
				this.taskType = 0
				if(window.Cordova) {
					let param = {}
					param.processId = index // 0 不到位拍照
					param.fileType = 0
					param.processName = name
					param.taskId = this.pohtoParam.taskId
					param.towerId = this.pohtoParam.towerId
					param.towerTaskId = this.pohtoParam.towerTaskId
					param.jd = jdwd.lon
					param.wd = jdwd.lat
					fileData('/fileserver/PictureZcXs/tsXsUploadPicture', 'post', param, filePath).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath
						})
						
					})
				} else {
					let param = new FormData();
					param.append("processId", index) // 0 不到位拍照
					param.append("fileType", 0)
					param.append("processName", name)
					param.append('taskId', this.pohtoParam.taskId)
					param.append('towerId', this.pohtoParam.towerId)
					param.append('towerTaskId', this.pohtoParam.towerTaskId)
					param.append('jd', '00.000000')
					param.append('wd', '00.000000')
					param.append('file', filePath)
					fileData('/fileserver/PictureZcXs/tsXsUploadPicture', 'post', param).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath
						})
					})
				}
			},
			// 保电值守拍照
			bdzsPhotos(index, name, filePath, jdwd) {
				this.taskType = 2
				if(window.Cordova) {
					let param = {}
					param.processId = index + 1
					param.fileType = 0
					param.towerTaskId = this.pohtoParam.towerTaskId
					param.towerId = this.pohtoParam.towerId
					param.processName = name
					param.jd = jdwd.lon
					param.wd = jdwd.lat
					fileData('/fileserver/PictureZsTask/tsXsUploadPicture', 'post', param, filePath).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath,
							'taskType':2
						})
					})
				} else {
					let param = new FormData();
					param.append("processId", index + 1)
					param.append('fileType', 0) // 0 图片
					param.append('towerTaskId', this.pohtoParam.towerTaskId)
					param.append('towerId', this.pohtoParam.towerId)
					param.append('processName', name)
					param.append('jd', '00.000000')
					param.append('wd', '00.000000')
					param.append('file', filePath)
					fileData('/fileserver/PictureZsTask/tsXsUploadPicture', 'post', param).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath,
							'taskType':2
						})
					})
				}
			},
			// 特殊巡视拍照
			tsxsPhotos(index, name, filePath, jdwd) {
				this.taskType = 1
				if(window.Cordova) {
					let param = {}
					param.processId = index + 1
					param.taskId = this.pohtoParam.taskId
					param.towerTaskId = this.pohtoParam.towerTaskId
					param.towerId = this.pohtoParam.towerId
					param.processName = name
					param.jd = jdwd.lon
					param.wd = jdwd.lat
					fileData('/fileserver/PictureTsXs/tsXsUploadPicture', 'post', param, filePath).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath,
							'taskType':1
						})
					})
				} else {
					let param = new FormData();
					param.append("processId", index + 1)
					param.append('taskId', this.pohtoParam.taskId)
					param.append('towerTaskId', this.pohtoParam.towerTaskId)
					param.append('towerId', this.pohtoParam.towerId)
					param.append('processName', name)
					param.append('jd', '00.000000')
					param.append('wd', '00.000000')
					param.append('file', filePath)
					fileData('/fileserver/PictureTsXs/tsXsUploadPicture', 'post', param).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath,
							'taskType':1
						})
					})
				}
			},
			// 线路检修拍照
			xljxPhotos(index, name, filePath, jdwd) {
				this.taskType = 3
				if(window.Cordova) {
					let param = {}
					param.processId = index + 1
					param.taskId = this.pohtoParam.taskId
					param.towerTaskId = this.pohtoParam.towerTaskId
					param.towerId = this.pohtoParam.towerId
					param.processName = name
					param.jd = jdwd.lon
					param.wd = jdwd.lat  
					fileData('/fileserver/PictureJx/jxUploadPicture', 'post', param, filePath).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath,
							'taskType':3
						})
					})
				} else {
					let param = new FormData();
					param.append("processId", index + 1)
					param.append('taskId', this.pohtoParam.taskId)
					param.append('towerTaskId', this.pohtoParam.towerTaskId)
					param.append('towerId', this.pohtoParam.towerId)
					param.append('processName', name)
					param.append('jd', '00.000000')
					param.append('wd', '00.000000')
					param.append('file', filePath)
					fileData('/fileserver/PictureJx/jxUploadPicture', 'post', param).then(res => {
						if(res.success) this.photoLists[index].photo.push({
							'photo': fileUrl(res.data.filePath),
							'path': res.data.filePath,
							'taskType':3
						})
					})
				}
			},
			// 照片回显
			echoImages() {
				if(this.pohtoShow.type == 'cgxs') this.cgxsImages()
				// else if(this.pohtoShow.type == 'cgxsIsDw') this.isDWImages() //常规巡视不到位照片
				else if(this.pohtoShow.type == 'bdzs') this.bdzsImages()
				else if(this.pohtoShow.type == 'tsxs') this.tsxsImages()
				else if(this.pohtoShow.type == 'xljx') this.xljxImages()
			},
			// 常规巡视照片回显
			cgxsImages() {
				var _this = this;
				var towerTaskId = _this.pohtoParam.towerTaskId,
					towerId = _this.pohtoParam.towerId,
					status = _this.pohtoParam.status;
				//判断任务状态为进行中、已完成 则回显照片
				var param = {};
					param.towerTaskId = towerTaskId,
					param.towerId = towerId;
				callData('/xsTaskServer/ZcXsTowerTask/getPictures', 'get', param).then(res => {
					_this.allImagesShow(res)
				})
			},
			// 不到位照片回显添加完成之后
			/* isDWImages(item,i,index){
				var _this = this;
				var photoLists = _this.photoLists;
				console.log(photoLists,"photoLists1");
				photoLists[index].photo.splice(i,1);
				console.log(photoLists[index].photo.splice(i,1));
			}, */
			// 保电值守照片回显
			bdzsImages() {
				const _this = this
				var towerTaskId = _this.pohtoParam.towerTaskId,
					towerId = _this.pohtoParam.towerId,
					status = _this.pohtoParam.status, //任务状态
					towerStatus = _this.pohtoParam.towerStatus; //杆塔状态
				//判断status任务状态为进行中并且towerStatus杆塔状态为进行中1 || status任务状态为已完成并且towerStatus杆塔状态为进行中，则回显照片
				var param = {};
					param.towerTaskId = towerTaskId,
					param.towerId = towerId;
				callData("/zsTaskServer/ZsBdTowerTask/getPictures", 'get', param).then(res => {
					if(res.success) {
						_this.allImagesShow(res)
					}
				})
			},
			// 特殊巡视照片回显
			tsxsImages() {
				const _this = this
				const param = {
					taskId: _this.pohtoParam.taskId,
					towerTaskId: _this.pohtoParam.towerTaskId,
					cmTowerId: _this.pohtoParam.towerId
				}
				callData('/tsxstaskServer/TsXsTowerTask/findTsXsPicture', 'get', param).then(res => {
					_this.allImagesShow(res)
				})
			},
			// 线路检修照片回显
			xljxImages() {
				const _this = this
				const param = {
					taskId: _this.pohtoParam.taskId,
					towerTaskId: _this.pohtoParam.towerTaskId,
					cmTowerId: _this.pohtoParam.towerId
				}
				callData('/jxtaskServer/JxTowerTask/findJxPicture', 'get', param).then(res => {
					_this.allImagesShow(res)
				})
			},
			allImagesShow(res) {
				let _this = this
				if(res == 'noPhotos') {
					this.$emit('quexian_data', 9);
				} else {
					let photoLists = this.photoLists
					let filePath = res.data.picture ? res.data.picture : res.data.PICTURE;
					let resMap = {}
					filePath.forEach(v => {
						let _imUrl = fileUrl(v.FILE_PATH)
						let _path = v.FILE_PATH
						if(!resMap[v.PROCESS_ID]) { 
							resMap[v.PROCESS_ID] = [{
								photo: _imUrl,
								path: _path,
								taskType: _this.taskType
							}]
						} else {
							resMap[v.PROCESS_ID].push({
								photo: _imUrl,
								path: _path,
								taskType: _this.taskType
							})
						}
					})
					photoLists.forEach((v, i) => {
						let photos = []
						if(resMap[i + 1]) {
							resMap[i + 1].forEach(rv => {
								photos.push({
									photo: rv.photo,
									path: rv.path,
									taskType: rv.taskType
								})
							})
						}
						photoLists[i].photo = photos
					})
					// 缺陷传参
					let quexian = res.data;
					this.$emit('quexian_data', quexian);
				}
			}
		},
		mounted() {
			this.getPhotoImg() // 判断需要上面图片
			this.isAndroid() // 判断是否是安卓环境
			this.echoImages() //照片回显
			eventbus.$on('scrollBottom',function(val){
				setTimeout(function(){
					document.scrollingElement.scrollTop = document.scrollingElement.scrollHeight;
				},320)
			})
		},
		beforeDestroy () {
			eventbus.$off('scrollBottom',1)
		}
	}
</script>